<?php

class LienheController extends Zend_Controller_Action {

	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	//Duong dan cua Controller
	protected $_currentController;

	//Duong dan cua Action chinh
	protected $_actionMain;
	
	public function init() {
		$file_uri = $this->view->baseUrl(DEFAULT_PATH . '/js/jquery.dd.js');
	
		// Meta
		$this->view->headMeta()->setName('description', 'Trang web chuyên về thông tin phòng trọ.');
		$this->view->headMeta()->appendName('keywords', 'phòng trọ, phòng cho thuê, thuê phòng, nhà trọ');

		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

		//Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';
				
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	}

	public function indexAction() {
		// Title
		$this->view->headTitle('Liên hệ');

		//maps google
		$this->view->headScript()->appendFile('https://maps.googleapis.com/maps/api/js?key=AIzaSyDypXt_xTgAMOTpSEdwmPXjUA0JGGbyXhY&sensor=false');
		$file_uri = $this->view->baseUrl(PUBLIC_PATH . '/js/google.map.js');
		$this->view->headScript()->appendFile($file_uri);
		
		$model = new Default_Model_ChiNhanhMapper();
		$chi_nhanh = $model->fetch('list');
		$this->view->chi_nhanh = $chi_nhanh;
		
		$model = new Default_Model_ThongTinCtyMapper();
		$thong_tin_cty = $model->fetchAll();
		$this->view->thong_tin_cty = $thong_tin_cty;

		$this->view->bodyScripts = 'onload="displayLocation(\'' . $thong_tin_cty['cty_dia_chi'] . '\')"';
		
		$form = new Default_Form_Contact();
		$form->submit->setLabel('Gửi email');
		$form->reset->setLabel('Hủy');
		$this->view->frmContact = $form;
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$mail = new Zend_Mail('utf-8');
                $mail->addTo('support@kenhnhatro.net', 'Kênh nhà trọ')
                	 ->setFrom($formData['email'], $formData['ho_ten'])
                	 ->setReplyTo($formData['email'], $formData['ho_ten'])
                     ->setSubject($formData['tieu_de'])
                     ->setBodyHtml('Email người gửi ' . $formData['email'] . ' <br> ' . $formData['noi_dung']);
                $result = $mail->send();
                $form->reset();
			} else {
				$form->populate($formData);
			}
		}
		$mask = APPLICATION_PATH . "/../public/captcha/*.png";
		array_map("unlink", glob($mask));
		
	}
	
}